package com.tian.netty.server;

import com.tian.netty.code.MyDecoder;
import com.tian.netty.util.EhcacheUtil;
import io.netty.channel.ChannelId;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import io.netty.handler.timeout.IdleStateHandler;

import java.net.InetSocketAddress;

/**
 * @author tianwc  公众号：java后端技术全栈、面试专栏
 * @version 1.0.0
 * @date 2023年06月20日 08:56
 * 博客地址：<a href="http://woaijava.cc/">博客地址</a>
 */
public class NettyServerChannelInitializer extends ChannelInitializer<SocketChannel> {


    @Override
    protected void initChannel(SocketChannel channel) throws Exception {

        EhcacheUtil ehcache = EhcacheUtil.getInstance();
        //channel.pipeline().addLast("decoder",new StringDecoder(CharsetUtil.UTF_8));
        //channel.pipeline().addLast("encoder",new StringEncoder(CharsetUtil.UTF_8));
        channel.pipeline().addLast("decoder", new ByteArrayDecoder());
        channel.pipeline().addLast("encoder", new ByteArrayEncoder());


        //超时时间 60秒
        channel.pipeline().addLast(new IdleStateHandler(60, 0, 0));
//
        channel.pipeline().addLast(new MyDecoder());
        channel.pipeline().addLast(new NettyServerHandler()); // 心跳 签到
        channel.pipeline().addLast("handler", new NettySystemHandler()); //逻辑
        //channel.pipeline().addLast(HandleName.HANDLE_CHARGE,new NettyChargeHandler());
        channel.pipeline().addLast("charge", new NettyChargeHandler());


        InetSocketAddress insocket = (InetSocketAddress) channel.remoteAddress();
        String clientIp = insocket.getAddress().getHostAddress();
        ChannelId channelId = channel.id(); // 获取连接通道唯一标识通道号

        ehcache.put(clientIp + channelId, channel.pipeline().get("charge"));


    }
}
